import { Toast } from "antd-mobile";
import store from "../../Store/Store";
import { InterfaceG } from "../../../config/Config";
import yhTools from "../../../tools/yh";

import { setOCRInfo } from "../liveness/index";

export const SET_SUPPORTED_BANK_LIST = "SET_SUPPORTED_BANK_LIST";

export const SET_SELECTED_BANK_CARD = "SET_SELECTED_BANK_CARD";

export const ADD_NEW_BANK = "ADD_NEW_BANK";

export const SET_SELECTED_BANK_LIST = "SET_SELECTED_BANK_LIST";

export const SET_BONUS = "SET_BONUS";

export const setSupportedBankList = data => ({
	type: SET_SUPPORTED_BANK_LIST,
	data
});

export const setSelectedBankList = data => ({
	type: SET_SELECTED_BANK_LIST,
	data
});

export const setSelectedBankCard = data => ({
	type: SET_SELECTED_BANK_CARD,
	data
});

export const addNewBank = data => ({
	type: ADD_NEW_BANK,
	data
});

export const setBonus = data => ({
	type: SET_BONUS,
	data
});

export const getAllSupportedBankList = ({ channelCode }) => {
	// Toast.loading("加载中...", 100);
	let obj = {
		url: InterfaceG.supportedBankcardList,
		params: {
			channelCode
		},
		_alertShow: false
	};
	return new Promise(resolve => {
		yhTools.request(obj).then(res => {
			if (res.code === "0") {
				Toast.hide();
				resolve(res.data);
			} else {
				Toast.info(res.message, 2);
			}
		});
	});
};

export const getBankInfo = () => {
	return new Promise(resolve => {
		yhTools
			.request({
				url: InterfaceG.checkUserBankInfo,
				params: {},
				_alertShow: false
			})
			.then(res => {
				if (res.code === "0") {
					resolve(res.data);
				}
			});
	});
};

export const getAllSelectedBankList = ({ channelCode }) => {
	let obj = {
		url: InterfaceG.myBankcardList,
		params: {
			channelCode
		},
		_alertShow: false
	};
	return new Promise(resolve => {
		yhTools.request(obj).then(res => {
			if (res.code === "0") {
				resolve(res.data);
			} else {
				Toast.info(res.message, 2);
			}
		});
	});

	// yhTools.request(obj).then(res => {
	//     if (res.code === "0") {
	//         return Promise.resolve(res.data);
	//     } else {
	//         Toast.info(res.message, 2);
	//     }
	// });
};

export const getSupportedBankList = (params, fn) => {
	return async dispatch => {
		const res = await getAllSupportedBankList(params);
		dispatch(setSupportedBankList(res));
		fn && fn();
	};
};

export const getSelectedBankList = (params, fn) => {
	return async dispatch => {
		const res = await getAllSelectedBankList(params);
		dispatch(setSelectedBankList(res));
		fn && fn();
	};
};

export const checkUserBankInfo = (params, fn) => {
	return async dispatch => {
		const res = await getBankInfo();
		dispatch(getSupportedBankList({ channelCode: params.channelCode }, fn));
		dispatch(setOCRInfo({ userName: res.userName }));
	};
};

/**
 *  授信步骤编号
		CODE_00("602", "60201", "四要素"),
		CODE_01("602", "60202", "人脸识别"),
		CODE_02("602", "60203", "完善资料"),
		CODE_04("603", "60301", "二要素"),
		CODE_05("603", "60302", "人脸识别"),
		CODE_06("604", "60401", "人脸识别"),
		CODE_07("606", "60601", "二要素"),
		CODE_08("607", "60701", "二要素"),
		CODE_09("607", "60702","完善资料"),
		CODE_10("608", "60801","二要素"),
		CODE_11("608", "60802","人脸识别"),
		CODE_12("608", "60803", "绑卡"),
		CODE_13("609", "60901", "二要素"),
		CODE_14("609", "60902", "人脸识别"),
		CODE_15("609", "60903", "绑卡"),
		CODE_16("609", "60904", "完善资料"),
		CODE_17("610", "61001", "四要素"),
		CODE_18("610", "61002", "人脸识别"),
		CODE_19("611", "61101", "四要素"),
		CODE_20("611", "61102", "人脸识别"),
		CODE_21("611", "61103", "完善资料"),
 * */
export const getBonus = step => {
	console.log("【奖励步骤】：", step);
	let obj = {
		url: InterfaceG.xhfCheckBonus,
		params: {
			applyNo: store.getState().getAppPlatformsDatas.applyNo,
			version: "1.0",
			stepCode: step
		},
		_alertShow: false
	};
	return new Promise(resolve => {
		yhTools.request(obj).then(res => {
			resolve(res.data);
			if (res.code !== "0") {
				if (res.message) Toast.info(res.message, 2);
			}
		});
	});
};

// 获取奖励
export const checkBonus = (step, cb) => {
	return async dispatch => {
		const res = await getBonus(step);
		// 授信步骤奖励开启状态
		const bonusStr = res.resultMsg ? res.resultMsg : "";
		dispatch(setBonus(bonusStr));
		cb && cb();
	};
};
